package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.ExChangeP;
import com.cljz.makepassenger.widget.XRadioGroup;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by CLJZ-fyw on 2017/6/6.
 */

public class ExchangeActivity extends BaseActivity<ExChangeP> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_point_total)
    TextView tvPoint;
    @BindView(R.id.btn_exchange)
    Button btnExchange;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.iv_line)
    ImageView ivLine;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.rb_100)
    RadioButton rb100;
    @BindView(R.id.rb_200)
    RadioButton rb200;
    @BindView(R.id.rb_500)
    RadioButton rb500;
    @BindView(R.id.rb_1000)
    RadioButton rb1000;
    @BindView(R.id.tv_exchange_des)
    TextView tvExchangeDes;
    @BindView(R.id.xrg_points)
    XRadioGroup xrgPoints;
    private int point;


    private String TAG = getClass().getSimpleName();
    private String selectPoint = "0";

    @Override
    public void initData(Bundle savedInstanceState) {
        btnExchange.setEnabled(false);
        tvTitleName.setText("积分兑换");
        tvExchangeDes.setText("(100积分可以兑换￥" + 100 / Integer.parseInt(getP().getLocalUserInfo(getP().getToken()).getRatio()) + ")");
        String myPoint = getIntent().getStringExtra("myPoints");
        point = (myPoint == null || "".equals(myPoint)) ? 0 : Integer.parseInt(myPoint);
        tvPoint.setText(String.valueOf(point));
        setRadioButton();
    }

    private void setRadioButton() {
        if (point < 100) {
            rb100.setEnabled(false);
            rb200.setEnabled(false);
            rb500.setEnabled(false);
            rb1000.setEnabled(false);
        } else if (point < 200) {
            rb100.setEnabled(true);
            rb200.setEnabled(false);
            rb500.setEnabled(false);
            rb1000.setEnabled(false);
        } else if (point < 500) {
            rb100.setEnabled(true);
            rb200.setEnabled(true);
            rb500.setEnabled(false);
            rb1000.setEnabled(false);
        } else if (point < 1000) {
            rb100.setEnabled(true);
            rb200.setEnabled(true);
            rb500.setEnabled(true);
            rb1000.setEnabled(false);
        } else {
            rb100.setEnabled(true);
            rb200.setEnabled(true);
            rb500.setEnabled(true);
            rb1000.setEnabled(true);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_exchangepoint;
    }

    @Override
    public ExChangeP newP() {
        return new ExChangeP();
    }

    @Override
    protected void initListener() {
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
        RxView.clicks(btnExchange)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        getP().exchangePoint(selectPoint);
                    }
                });

        xrgPoints.setOnCheckedChangeListener(new XRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(XRadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_100:
                        selectPoint = "100";
                        break;
                    case R.id.rb_200:
                        selectPoint = "200";
                        break;
                    case R.id.rb_500:
                        selectPoint = "500";
                        break;
                    case R.id.rb_1000:
                        selectPoint = "1000";
                        break;
                }
                btnExchange.setEnabled(true);
            }
        });
    }

    public void exchangeSuccess() {
        Router.newIntent(this)
                .putInt("exchange", Integer.valueOf(selectPoint))
                .putInt("ratio", Integer.parseInt(getP().getLocalUserInfo(getP().getToken()).getRatio()))
                .to(PointsDetailsActivity.class).launch();
    }

    public void exchangeFailed(String error) {
        showToast(error, R.mipmap.jinshi);
    }
}
